package com.yibao.controller;

import com.yibao.model.User;
import com.yibao.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2024-06-25 14:58
 */
@RestController
@RequestMapping("/api/user/")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 注册用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("保存用户信息")
    public ResponseEntity<String> saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return ResponseEntity.ok("保存成功");
    }

    /**
     * 查询用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation("查询用户信息")
    @GetMapping("/{id}")
    public ResponseEntity<User> getUser(@PathVariable Long id) {
        User user = userService.getUserInfo(id);
        return ResponseEntity.ok(user);
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询用户list")
    public ResponseEntity<List<User>> getUserList() {
        List<User> users = userService.list();
        return ResponseEntity.ok(users);
    }

}
